<?php

/*
 * 店铺
 */

class dbt_store extends sing_table
{
    private $_mid;
    private $_G;
    private $_openid;
    private $_memberId;
    private $_DBstore;

    //构造方法
    function __construct()
    {
        global $_G;
        $this->_G = &$_G;
        $this->_mid = $_GET['mid'];
        $this->_openid = $this->_G[$this->_mid]['openid'];
        $this->_memberId = $this->_G[$this->_mid]['memberid'];

        $this->_DBstore = 'es_store';
    }

    /*
    *读取店铺总数信息
    */
    public function getStoreCount($where='', $limit=''){

        DB::SetMainDB(DB_ESHOP);
        $r = DB::result_first( 'select count(1) from %t where mid=%s %i %i', array ($this->_DBstore, $this->_mid, $where, $limit));
        return $r;
    }
    //获取一个子店铺全部信息
    public function storeInfo($store_id){
        DB::SetMainDB(DB_ESHOP);
        $r = DB::fetch_first('select `title` from %t where id = %d',array($store_id));
        return $r;
    }

    /*
     * 获取所有店铺
     */
    public function getStoreAll( $where='', $limit='')
    {
        DB::SetMainDB(DB_ESHOP);
        $r = DB::fetch_all( 'select id,title,classify_id,thumb,content,phone,level from %t where display=1 and status=1 and mid=%s %i %i', array ($this->_DBstore, $this->_mid, $where, $limit));
        return $r;
    }


    public function getStore($where=''){
        if($where) $where = ' and '.$where;
        DB::SetMainDB(DB_ESHOP);
        $r = DB::fetch_all( 'select id,title,classify_id,thumb,content,phone,level from %t where mid=%s %i', array ($this->_DBstore, $this->_mid, $where));
        $lists = array();
        foreach($r as $v){
            $lists[$v['id']] = $v;
        }
        return $lists;
    }
    /*获取所有店铺信息*/
    public function getStoreInfos()
    {
        DB::SetMainDB(DB_ESHOP);
        $storeInfo = DB::fetch_all('select * from %t where  mid= %s and display =1',array('es_store',$this->_mid));
        return $storeInfo;
    }

    /*
     * 获取单个店铺
     */
    public function getStoreInfo( $where='' )
    {
        DB::SetMainDB(DB_ESHOP);
        $r = DB::fetch_first( 'select id,is_self from %t where display=1 and status=1 and mid=%s %i', array ($this->_DBstore, $this->_mid, $where));
        return $r;
    }

    public function getStoreM($_id,$_upmemory = 0){
        if(!$_upmemory) $r = memory('get','es-store-'.$this->_mid.'-'.$_id);
        if(!$r) {
            DB::SetMainDB(DB_ESHOP);
            $r = DB::fetch_first('select * from %t where id=%d and mid=%s',array($this->_DBstore, $_id, $this->_mid));
            if($r) memory('set','es-store-'.$this->_mid.'-'.$_id,$r);
        }
        return $r;
    }

    public function getCategoryM($id=0,$upmemory = 0){
        if(!$this->_mid || !$id) return false;
        if(!$upmemory) $r = memory('get','es-category-'.$mid.'-'.$id);
        if (!$r) {
            DB::SetMainDB(DB_ESHOP);
            $r = DB::fetch_first('select * from %t where id=%d and display=1 and mid=%s', array($this->_DBstore, $id, $this->_mid));
            if($r) memory('set','es-category-'.$mid.'-'.$id,$r);
        }
        return $r;
    }
}